package com.sihan.swvp.action.sys;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.common.SvgUtil;
@Controller
@RequestMapping("/system")
public class SystemAction extends BaseAction{

	@RequestMapping(value="/getCss")
	public void getCss(HttpServletResponse response){
		try {
			response.setContentType("text/css;charset=UTF-8");  
	        OutputStream ostream = response.getOutputStream();   
	        String css = ".nav-node div{font-size: 18px;	font-family: '华文彩云';	color: red;}";
	        ostream.write(css.getBytes("utf-8")); 
	        ostream.flush();
	        ostream.close();  
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/saveChart")
	public void saveChart(HttpServletResponse response,@RequestParam String svg){
		OutputStream output = null;
		InputStream in = null;
		try {
			output = response.getOutputStream();
			byte[] bytes = SvgUtil.svg2ipng(svg);
			String fileName = URLEncoder.encode("chart.png", "UTF-8");  
			response.reset();  
		    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");  
		    response.addHeader("Content-Length", "" + bytes.length);  
		    response.setContentType("application/octet-stream;charset=UTF-8");  
		    output = new BufferedOutputStream(response.getOutputStream());  
			output.write(bytes);
		    output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		finally{
			CommonUtil.close(output);
			CommonUtil.close(in);
		}
	} 
	
}
